<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Controller for managing csv files.
 * @package backend-roles/conrollers
 * @category Controller
 * 
 * @author Mark Angelo Angulo
 * @copyright (c) 2015
 */
class Questionnaire extends CI_Controller {

    /**
     *  Constructor method.
     */
    function __construct()
    {
        parent::__construct();
        
        $this->load->helper('cms');
        
        $this->load->model('choices_model');
        $this->load->model('i_categories_model');
        $this->load->model('i_types_model');
        $this->load->model('item_categories_model');
        $this->load->model('item_types_model');
        $this->load->model('items_model');
        $this->load->model('item_questions_model');
        $this->load->model('q_types_model');
        $this->load->model('qr_categories_model');
        $this->load->model('qr_types_model');
        $this->load->model('question_choices_model');
        $this->load->model('question_hierarchy_model');
        $this->load->model('question_types_model');
        $this->load->model('questionnaire_categories_model');
        $this->load->model('questionnaire_items_model');
        $this->load->model('questionnaire_sports_model');
        $this->load->model('questionnaire_types_model');
        $this->load->model('questionnaires_model');
        $this->load->model('questions_model');
        $this->load->model('sports_model');


        $this->load->library('csvreader');
        $this->load->library('form_validation');
//        $this->form_validation->set_error_delimiters('<div class="clear-fix"></div><small class="help-block">', '</small></div>');
        $this->form_validation->set_error_delimiters('<div class="clear-fix"></div><small>', '</small>');

    }

    /**
     * Default method called when nothing is specified.
     * Displays dashboard.
     * @access public
     */
    public function index()
    {
        /*
         * @TODO
         * Script Limitations
         * Unable to distinguish same item with same type or category but different child question
         * Unable to distinguish same question with same choices but different child question
         */
        
        set_time_limit(0);

        $this->csvreader->separator = '|';
        $questionnaires = $this->csvreader->parse_file('csvs/Questionnaires.csv');

        $item_rank                          = 1;
        $question_rank                      = 1;
        $choice_rank                        = 1;
        $current_questionnaire_id           = NULL;
        $questionnaire_name                 = NULL;
        $item_name                          = NULL;
        $question_name                      = NULL;
        
        foreach($questionnaires as $que)
        {
            $questionnaire_id               = NULL;
            $qr_category_id                 = NULL;
            $qr_type_id                     = NULL;
            $item_id                        = NULL;
            $i_category_id                  = NULL;
            $i_type_id                      = NULL;

            // sports
            $sport_text = trim( $que['sport'] );
            if( $sport_text )
            {
                $sport                              = array();
                $sport['Name']                      = $sport_text;
                $sport['Description']               = isset( $que['sport_description'] )? trim( $que['sport_description'] ) : NULL;
                $sport['DateCreated']               = date('Y-n-d H:i:s');
                $sport_id = $this->sports_model->push( array('Name' => $sport_text), $sport);
            }
            
            // qr_categories
            $questionnaire_category_text = trim( $que['qr_category'] );
            if( $questionnaire_category_text )
            {
                $qr_category                              = array();
                $qr_category['Name']                      = $questionnaire_category_text;
                $qr_category['DateCreated']               = date('Y-n-d H:i:s');
                $qr_category_id = $this->qr_categories_model->push( array('Name' => $questionnaire_category_text), $qr_category);
            }
            
            // qr_types
            $questionnaire_type_text = trim( $que['qr_type'] );
            if( $questionnaire_type_text )
            {
                $qr_type                              = array();
                $qr_type['Name']                      = $questionnaire_type_text;
                $qr_type['DateCreated']               = date('Y-n-d H:i:s');
                $qr_type_id = $this->qr_types_model->push( array('Name' => $questionnaire_type_text), $qr_type);
            }
            
            // questionnaires
            $questionnaire_text = trim( $que['questionnaire'] );
            if( $questionnaire_text )
            {
                $questionnaire                              = array();
                $questionnaire['Name']                      = $questionnaire_text;
                $questionnaire['Description']               = isset( $que['qr_description'] )? trim( $que['qr_description'] ) : NULL;
                $questionnaire['DateCreated']               = date('Y-n-d H:i:s');
                $questionnaire_id = $this->questionnaires_model->push( array('Name' => $questionnaire_text), $sport_id, $qr_type_id, $qr_category_id, $questionnaire);
            
                if( $questionnaire_text != $questionnaire_name )
                {
                    $item_rank                  = 1;
                    $questionnaire_name         = $questionnaire_text;
                    $current_questionnaire_id   = $questionnaire_id;
                }
            }
            
            // questionnaire_sports
            if( $questionnaire_id && $sport_id )
            {
                $questionnaire_sport                     = array();
                $questionnaire_sport['QuestionnaireID']  = $questionnaire_id;
                $questionnaire_sport['SportID']          = $sport_id;
                $questionnaire_sport['DateCreated']      = date('Y-n-d H:i:s');
                $this->questionnaire_sports_model->push( array('QuestionnaireID' => $questionnaire_id, 'SportID' => $sport_id), $questionnaire_sport );
            }
            
            // questionnaire_categories
            if( $questionnaire_id && $qr_category_id )
            {
                $questionnaire_category                     = array();
                $questionnaire_category['QuestionnaireID']  = $questionnaire_id;
                $questionnaire_category['QRCategoryID']     = $qr_category_id;
                $questionnaire_category['DateCreated']      = date('Y-n-d H:i:s');
                $this->questionnaire_categories_model->push( array('QuestionnaireID' => $questionnaire_id, 'QRCategoryID' => $qr_category_id), $questionnaire_category );
            }

            // questionnaire_types
            if( $questionnaire_id && $qr_type_id )
            {
                $questionnaire_type                     = array();
                $questionnaire_type['QuestionnaireID']  = $questionnaire_id;
                $questionnaire_type['QRTypeID']         = $qr_type_id;
                $questionnaire_type['DateCreated']      = date('Y-n-d H:i:s');
                $this->questionnaire_types_model->push( array('QuestionnaireID' => $questionnaire_id, 'QRTypeID' => $qr_type_id), $questionnaire_type );
            }
            
            // i_categories
            $item_category_text = trim( $que['i_category'] );
            if( $item_category_text )
            {
                $i_category                              = array();
                $i_category['Name']                      = $item_category_text;
                $i_category['DateCreated']               = date('Y-n-d H:i:s');
                $i_category_id = $this->i_categories_model->push( array('Name' => $item_category_text), $i_category);
            }
            
            // i_types
            $item_type_text = trim( $que['i_type'] );
            if( $item_type_text )
            {
                $i_type                              = array();
                $i_type['Name']                      = $item_type_text;
                $i_type['DateCreated']               = date('Y-n-d H:i:s');
                $i_type_id = $this->i_types_model->push( array('Name' => $item_type_text), $i_type);
            }
            
            // items
            $item_text = trim( $que['item'] );
            if( $item_text )
            {
                $item                              = array();
                $item['Name']                      = $item_text;
                $item['DateCreated']               = date('Y-n-d H:i:s');
                $item_id = $this->items_model->push( array('Name' => $item_text), $item);
                
                if( $item_text != $item_name )
                {
                    $question_rank              = 1;
                    $item_name                  = $item_text;
                }
            }
            
            // item_categories
            if( $item_id && $i_category_id )
            {
                $item_category                      = array();
                $item_category['ItemID']            = $item_id;
                $item_category['ICategoryID']       = $i_category_id;
                $item_category['DateCreated']       = date('Y-n-d H:i:s');
                $this->item_categories_model->push( $item_category, $item_category );
            }

            // item_types
            if( $item_id && $i_type_id )
            {
                $item_type                      = array();
                $item_type['ItemID']            = $item_id;
                $item_type['ITypeID']           = $i_type_id;
                $this->item_types_model->push( $item_type, $item_type );
            }
            
            // questionnaire_items
            if( $current_questionnaire_id && $item_id )
            {
                $questionnaire_item                      = array();
                $questionnaire_item['QuestionnaireID']   = $current_questionnaire_id;
                $questionnaire_item['ItemID']            = $item_id;
                $questionnaire_item['Rank']              = $item_rank;
                $questionnaire_item['DateCreated']       = date('Y-n-d H:i:s');
                $this->questionnaire_items_model->push( array('QuestionnaireID' => $current_questionnaire_id, 'ItemID' => $item_id), $questionnaire_item );

                $item_rank += 1;
            }
            
            // q_types
            $question_type_text = trim( $que['q_type'] );
            if( $question_type_text )
            {
                $q_type                              = array();
                $q_type['Name']                      = $question_type_text;
                $q_type['DateCreated']               = date('Y-n-d H:i:s');
                $q_type_id = $this->q_types_model->push( array('Name' => $question_type_text), $q_type);
            }
            
            // questions
            $question_text                  = trim( $que['question'] );
            $question                       = array();
            $question['Name']               = $question_text;
            $question['DateCreated']        = date('Y-n-d H:i:s');
            $question_id = $this->questions_model->insert($question);
//            $question_id = $this->questions_model->push_question( array('Name' => $question_text), $q_type_id, trim($que['choices']), $question);
            
            // question_types
            if( $question_id && $q_type_id )
            {
                $question_type                      = array();
                $question_type['QuestionID']        = $question_id;
                $question_type['QTypeID']           = $q_type_id;
                $question_type['DateCreated']       = date('Y-n-d H:i:s');
                $this->question_types_model->push( array('QuestionID' => $question_id, 'QTypeID' => $q_type_id), $question_type );
            }
            
            // question_hierarchy
            if( isset( $que['parent_question'] ) && isset( $que['choice_trigger'] ) && isset( $que['hierarchy'] ) && isset( $que['group'] ) )
            {
                $parent_question_id                     = NULL;
                $choice_trigger_id                      = NULL;
                $hierarchy                              = trim( $que['hierarchy'] );
                $group                                  = trim( $que['group'] );
                
                $parent_question_text                  = trim( $que['parent_question'] );
                if( $parent_question_text )
                {
                    $parent_question                       = array();
                    $parent_question['Name']               = $parent_question_text;
                    $parent_question['DateCreated']        = date('Y-n-d H:i:s');
                    $parent_question_id = $this->questions_model->push( array('Name' => $parent_question_text), $parent_question);
                }
                
                $choice_trigger_text                         = trim( $que['choice_trigger'] );
                if( $choice_trigger_text )
                {
                    $choice_trigger                              = array();
                    $choice_trigger['Name']                      = $choice_trigger_text;
                    $choice_trigger['DateCreated']               = date('Y-n-d H:i:s');
                    $choice_trigger_id = $this->choices_model->push( array('Name' => $choice_trigger_text), $choice_trigger);
                }
                
                if( $question_id && $parent_question_id && $choice_trigger_id && $hierarchy && $group )
                {
                    $question_hierarchy                         = array();
                    $question_hierarchy['QuestionID']           = $question_id;
                    $question_hierarchy['ParentQuestionID']     = $parent_question_id;
                    $question_hierarchy['ChoiceID']             = $choice_trigger_id;
                    $question_hierarchy['Rank']                 = $hierarchy;
                    $question_hierarchy['Group']                = $group;
                    $this->question_hierarchy_model->insert( $question_hierarchy );
                }
            }
            
            // item_questions
            if( $item_id && $question_id )
            {
                $item_question                      = array();
                $item_question['ItemID']            = $item_id;
                $item_question['QuestionID']        = $question_id;
                $item_question['Rank']              = $question_rank;
                $item_question['DateCreated']       = date('Y-n-d H:i:s');
                $this->item_questions_model->push( array('ItemID' => $item_id, 'QuestionID' => $question_id), $item_question );
                
                $question_rank += 1;
            }
            
            // choice
            $choice_text                                = trim( $que['choices'] );
            if( $choice_text )
            {
                $choices = explode(',', $choice_text);
                foreach( $choices as $cho )
                {
                    $cho                                 = trim( $cho );
                    $choice                              = array();
                    $choice['Name']                      = $cho;
                    $choice['DateCreated']               = date('Y-n-d H:i:s');
                    $choice_id = $this->choices_model->push( array('Name' => $cho), $choice);
                    
                    if( $question_text != $question_name )
                    {
                        $choice_rank              = 1;
                        $question_name            = $question_text;
                    }
                    
                    if( $question_id )
                    {
                        $question_choice                     = array();
                        $question_choice['QuestionID']       = $question_id;
                        $question_choice['ChoiceID']         = $choice_id;
                        $question_choice['Rank']             = $choice_rank;
                        $question_choice['DateCreated']      = date('Y-n-d H:i:s');
                        $this->question_choices_model->push( array('QuestionID' => $question_id, 'ChoiceID' => $choice_id), $question_choice );
                    
                        $choice_rank += 1;
                    }
                    
                }
            }
            
        }
        
        echo '<pre>';
        print_r($questionnaires); 
        echo '</pre>';
        exit();

    }
}

/* End of file parsecsv.php */
/* Location: ./application/roles/backend-csvreader/controllers/parsecsv.php */